/**
 * 统考成绩管理
 */

<template>
  <div class="courseBox">
    <div class="courseBoxTop">
      <div class="courseBoxTopSearch">
        <div class="courseName">
          <span>姓名：</span>
          <el-input
            v-model="searchForm.studentName"
            placeholder="请输入姓名"
            style=""
          />
        </div>
        <div class="profession">
          <span>性别：</span>
          <el-select v-model="searchForm.gender" placeholder="请选择性别">
            <el-option
              :label="item.typeName"
              :value="item.typeId"
              v-for="(item, index) in sexList"
              :key="index"
            >
            </el-option>
          </el-select>
        </div>
        <div class="profession">
          <span>证件类型：</span>
          <el-select
            v-model="searchForm.identificationType"
            placeholder="请选择证件类型"
          >
            <el-option
              v-for="(item, index) in identificationTypeList"
              :key="index"
              :label="item.typeName"
              :value="item.typeId"
            >
            </el-option>
          </el-select>
        </div>

        <div class="courseName">
          <span>证件号码：</span>
          <el-input
            v-model="searchForm.idCardNo"
            placeholder="请输入证件号码"
            style=""
          />
        </div>

        <div class="searchAndReset">
          <el-button class="searchBtn" @click="searchInput">
            <i class="el-icon-search"></i>
            <span>搜索</span>
          </el-button>
          <el-button class="resetBtn" @click="refreshInput">
            <i class="el-icon-refresh"></i>
            <span>重置</span>
          </el-button>
        </div>
      </div>
    </div>
    <div class="courseBoxBottom">
      <div class="bottomHeader">
        <span class="title">管理列表</span>
        <div class="bottomHeaderBtn">
          <el-button class="addBtn" @click="openAddClassify">
            <i class="el-icon-plus"></i>
            <span>新增</span>
          </el-button>
          <el-button class="delBtn" @click="batchDelClassGrade">
            <i class="el-icon-close"></i>
            <span>批量删除</span>
          </el-button>
          <!-- <el-button class="toLeadBtn" @click.stop="setIsMaskOpen">
            <span class="icon iconfont icon-daoru"></span>
            <span>批量导入</span> -->

          <!-- 弹框 -->
          <!-- <div
              class="toLeadBtnMask"
              v-if="isMask"
              @mouseenter="setIsMaskOpen"
              @mouseleave="setIsMaskClose"
            >
              <span class="sanjiao"></span>
              <div class="ul">
                <el-upload
                  class="upload-demo"
                  action="https://jsonplaceholder.typicode.com/posts/"
                  :on-preview="handlePreview"
                >
                  <div class="li">
                    <i></i>
                    <el-button size="small" type="primary" class="importCourse">
                      导入教师名单
                    </el-button>
                  </div>
                </el-upload>

                <div class="li">
                  <i></i>
                  <span>下载导入模板</span>
                </div>
              </div>
            </div>
          </el-button> -->

          <!-- <el-button class="exportBtn" @click="exportClassGrade">
            <span class="icon iconfont icon-daochu"></span>
            <span>导出数据</span>
          </el-button> -->
        </div>
      </div>
      <div class="bottomMian">
        <!-- 表格 -->
        <el-table
          ref="multipleTable"
          :data="tableData"
          tooltip-effect="dark"
          style="width: 100%"
          @selection-change="handleSelectionChange"
        >
          <el-table-column type="selection" width="50"> </el-table-column>
          <el-table-column
            type="index"
            label="序号"
            width="50"
          ></el-table-column>
          <el-table-column prop="studentName" label="姓名"></el-table-column>
          <el-table-column prop="gender" label="性别">
            <template slot-scope="scope">
              {{ scope.row.gender == "40" ? "男" : "女" }}
            </template>
          </el-table-column>
          <el-table-column
            prop="identificationTypeName"
            label="证件类型"
          ></el-table-column>
          <el-table-column prop="idCardNo" label="证件号码"></el-table-column>
          <el-table-column
            prop="subjectName"
            label="统考科目"
          ></el-table-column>
          <el-table-column prop="score" label="成绩"></el-table-column>
          <el-table-column prop="action" label="操作">
            <template slot-scope="scope">
              <span
                class="icon edit"
                @click="handleEdit(scope.row)"
              >编辑</span>
              <span
                class="icon del"
                @click="handleDel(scope.row.id)"
              >删除</span>
            </template>
          </el-table-column>
        </el-table>

        <!-- 分页 -->
        <div class="pagination" v-if="tableData.length > 0">
          <el-pagination
            @current-change="handleCurrentChange"
            :current-page="currentPage"
            :page-size="10"
            prev-text="上一页"
            next-text="下一页"
            layout=" prev, pager, next"
            :total="total"
          >
          </el-pagination>
          <div class="jumper">
            跳到第
            <input type="text" ref="currentPage" value="1" />
            页
          </div>
          <button class="submit" @click="goPage">确定</button>
        </div>
      </div>
    </div>

    <!-- 新增班级 -->
    <div class="dialog">
      <el-dialog
        :title="isEdit ? '编辑统考成绩' : '添加统考成绩'"
        :visible.sync="dialogFormVisible"
        center
      >
        <el-form :model="form" :rules="rules" ref="form">
          <el-form-item label="姓名:" :label-width="formLabelWidth">
            <el-input
              v-model="form.studentName"
              autocomplete="off"
              placeholder="请输入姓名(必填)"
              style="width: 270px"
            ></el-input>
          </el-form-item>
          <el-form-item label="性别:" :label-width="formLabelWidth">
            <el-select
              v-model="form.gender"
              placeholder="请选择性别(必填)"
              style="width: 270px"
            >
              <el-option
                :label="item.typeName"
                :value="item.typeId"
                v-for="(item, index) in sexList"
                :key="index"
              >
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="证件类型:" :label-width="formLabelWidth">
            <el-select
              v-model="form.identificationType"
              placeholder="请选择证件类型(必填)"
              style="width: 270px"
            >
              <el-option
                v-for="(item, index) in identificationTypeList"
                :key="index"
                :label="item.typeName"
                :value="item.typeId"
              >
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item
            label="证件号码:"
            :label-width="formLabelWidth"
            prop="idCardNo"
          >
            <el-input
              v-model="form.idCardNo"
              autocomplete="off"
              placeholder="请输入证件号码(必填)"
              style="width: 270px"
            ></el-input>
          </el-form-item>

          <el-form-item label="统考科目:" :label-width="formLabelWidth">
            <el-select
              v-model="form.subjectId"
              placeholder="请选择统考科目(必填)"
              style="width: 270px"
            >
              <el-option
                v-for="(item, index) in examsubjectsList"
                :key="index"
                :label="item.subjectName"
                :value="item.subjectId"
              >
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="分数:" :label-width="formLabelWidth">
            <el-input
              v-model="form.score"
              autocomplete="off"
              placeholder="请输入分数(必填)"
              style="width: 270px"
            ></el-input>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="closeAddClass">取 消</el-button>
          <el-button type="primary" @click="confirmAddClass">确 定</el-button>
        </div>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import "@/styles/picture.scss";
import { _debounce } from "@/utils/debounce";
import {
  addExamresultsListApi,
  delExamresultsListApi,
  getExamsubjectsListApi,
  editExamresultsListApi,
  getExamresultsManagementListApi,
} from "@/api/examManagement/examresultsManagement";
import { getClassifyListApi } from "@/api/basics/classify";
export default {
  data() {
    return {
      tableData: [],
      majorList: [],
      sexList: [],
      total: 0,
      multipleSelection: [],
      currentPage: 1,
      pageSize: 10,
      isMask: false,
      dialogFormVisible: false,
      searchForm: {
        studentName: "",
        gender: "",
        identificationType: "",
        idCardNo: "",
      },
      form: {
        id: "",
        studentName: "",
        gender: "",
        identificationType: "",
        idCardNo: "",
        subjectId: "",
        score: "",
      },
      formLabelWidth: "100px",
      isEdit: false,
      filename: "班级管理表",
      examsubjectsList: [],
      identificationTypeList: [],
      rules: {
        //  证件号码
        studentName: [{ required: true, message: "请输入姓名" }],
        idCardNo: [
          { required: true, message: "请输入证件号码", trigger: "change" },
          {
            min: 18,
            max: 18,
            message: "长度在18个字符",
            trigger: "change",
          },
        ],
      },
    };
  },

  mounted() {
    // 获取列表
    this.getExamresultsManagementList({
      pageNum: this.currentPage,
      pageSize: this.pageSize,
    });

    // 获取科目列表
    this.getExamsubjectsList();

    // 获取性别列表
    this.getSexList({
      parentId: 39,
    });

    // 获取证件类型列表
    this.getIdentificationTypeList({
      parentId: 139,
    });
  },

  methods: {
    // 获取班级列表
    getExamresultsManagementList(data) {
      getExamresultsManagementListApi(data).then((result) => {
        if (result.code === 200) {
          let data = result.rows.map((item) => {
            return {
              ...item,
              status: item.status == "0" ? true : false,
            };
          });

          this.tableData = data;
          this.total = result.total;
        }
      });
    },

    // 获取科目列表
    getExamsubjectsList() {
      getExamsubjectsListApi().then((res) => {
        if (res.code === 200) {
          this.examsubjectsList = res.data;
        }
      });
    },

    // 获取证件类型列表
    async getIdentificationTypeList(data) {
      let res = await getClassifyListApi(data);
      this.identificationTypeList = res.rows;
    },

    // 获取性别列表
    async getSexList(data) {
      let res = await getClassifyListApi(data);
      this.sexList = res.rows;
    },

    // 搜索
    searchInput: _debounce(function () {
      // 获取列表
      this.getExamresultsManagementList({
        pageNum: this.currentPage,
        pageSize: this.pageSize,
        ...this.searchForm,
      });
    }, 200),

    // 重置
    refreshInput: _debounce(function () {
      this.searchForm = {
        studentName: "",
        gender: "",
        identificationType: "",
        idCardNo: "",
      };
      this.getExamresultsManagementList({
        pageNum: this.currentPage,
        pageSize: this.pageSize,
      });
    }, 200),

    // 新增课程
    openAddClassify() {
      this.form = {
        id: "",
        studentName: "",
        gender: "",
        identificationType: "",
        idCardNo: "",
        subjectId: "",
        score: "",
      };
      this.isEdit = false;
      this.dialogFormVisible = true;
      this.$refs["form"].resetFields();
    },

    // 编辑课程
    handleEdit(data) {
      let {
        id,
        studentName,
        gender,
        identificationType,
        idCardNo,
        subjectId,
        score,
      } = data;
      this.form = {
        id,
        studentName,
        gender,
        identificationType,
        idCardNo,
        subjectId,
        score,
      };
      this.isEdit = true;
      this.dialogFormVisible = true;
    },

    // 取消
    closeAddClass() {
      this.dialogFormVisible = false;
      this.form = {
        id: "",
        studentName: "",
        gender: "",
        identificationType: "",
        idCardNo: "",
        subjectId: "",
        score: "",
      };
    },

    //确定新增 / 编辑
    confirmAddClass: _debounce(function () {
      if (this.isEdit) {
        if(this.form.studentName==''||this.form.gender==''||this.form.identificationType==''||
        this.form.idCardNo==''||this.form.subjectId==''||this.form.score==''){
             this.$message.success("请完善内容");
        }else{
          editExamresultsListApi(this.form).then((res) => {
          if (res.code === 200) {
            this.$message.success("修改成功！");
            this.dialogFormVisible = false;

            this.getExamresultsManagementList({
              pageNum: this.currentPage,
              pageSize: this.pageSize,
            });
          } else {
            this.$message.error("修改失败！");
          }
        });
        }
      
      } else {
        if(this.form.studentName==''||this.form.gender==''||this.form.identificationType==''||
        this.form.idCardNo==''||this.form.subjectId==''||this.form.score==''){
             this.$message.success("请完善内容");
        }else{
              addExamresultsListApi({ ...this.form, status: "0" }).then((res) => {
          if (res.code === 200) {
            this.$message.success("添加成功！");
            this.dialogFormVisible = false;

            this.getExamresultsManagementList({
              pageNum: this.currentPage,
              pageSize: this.pageSize,
            });
          } else {
            this.$message.error("添加失败！");
          }
        });
        }
        
      }
    }, 500),

    // 删除
    handleDel: _debounce(function (ids) {
      this.$confirm("此操作将删除该数据, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          delExamresultsListApi({ ids }).then((res) => {
            if (res.code === 200) {
              this.$message.success("删除成功！");
              this.getExamresultsManagementList({
                pageNum: this.currentPage,
                pageSize: this.pageSize,
              });
            } else {
              this.$message.error("删除失败！");
            }
          });
        })
        .catch(() => {});
    }, 500),

    // 批量删除
    batchDelClassGrade: _debounce(function () {
      if (this.multipleSelection.length) {
        let data = [...this.multipleSelection].map((item) => {
          return "" + item.id;
        });
        this.$confirm("此操作将删除已选择的数据, 是否继续?", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        })
          .then(() => {
            delExamresultsListApi({ ids: data.join(",") }).then((res) => {
              if (res.code === 200) {
                this.$message.success("删除成功！");
                this.getExamresultsManagementList({
                  pageNum: this.currentPage,
                  pageSize: this.pageSize,
                });
              } else {
                this.$message.error("删除失败！");
              }
            });
          })
          .catch(() => {});
      } else {
        this.$message.error("请选择要删除的数据！");
      }
    }, 500),

    // 导出班级数据
    // exportClassGrade() {
    //   this.$confirm("是否导出全部数据？", "提示", {
    //     confirmButtonText: "确定",
    //     cancelButtonText: "取消",
    //     type: "warning",
    //   })
    //     .then(() => {
    //       exportApi().then((res) => {
    //         if (!res) return;
    //         const blob = new Blob([res], {
    //           type: "application/vnd.ms-excel",
    //         }); // 构造一个blob对象来处理数据，并设置文件类型
    //         if (window.navigator.msSaveOrOpenBlob) {
    //           //兼容IE10
    //           navigator.msSaveBlob(blob, this.filename);
    //         } else {
    //           const href = URL.createObjectURL(blob); //创建新的URL表示指定的blob对象
    //           const a = document.createElement("a"); //创建a标签
    //           a.style.display = "none";
    //           a.href = href; // 指定下载链接
    //           a.download = this.filename; //指定下载文件名
    //           a.click(); //触发下载
    //           URL.revokeObjectURL(a.href); //释放URL对象
    //         }
    //       });
    //     })
    //     .catch(() => {});
    // },

    // // 打开导入弹框
    // setIsMaskOpen() {
    //   this.isMask = true;
    // },

    // // 关闭导入弹框
    // setIsMaskClose() {
    //   this.isMask = false;
    // },

    // // 导入上传
    // handlePreview(file) {
    //   console.log(file);
    // },

    // 已选中项
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },

    // 设置当前页
    handleCurrentChange(val) {
      this.currentPage = val;
      // 获取列表
      this.getExamresultsManagementList({
        pageNum: this.currentPage,
        pageSize: this.pageSize,
        ...this.searchForm,
      });
    },

    // 跳转指定页面
    goPage() {
      let { value } = this.$refs.currentPage;
      this.currentPage = Number(value);
      // 获取列表
      this.getExamresultsManagementList({
        pageNum: this.currentPage,
        pageSize: this.pageSize,
        ...this.searchForm,
      });
    },
  },
};
</script>

<style lang="scss" scoped>
.courseBoxTopSearch {
  ::v-deep .el-input__inner {
    height: 36px;
    line-height: 36px;
  }
}

.edit{
    color: #1990FF;
    cursor: pointer;
  }
  .del{
    color: #1990FF;
    margin-left: 10px;
    cursor: pointer;
  }
::v-deep .el-icon-date {
  line-height: 36px;
}

.block {
  height: 40px;
  border: solid 1px #dcdfe6;
  border-radius: 4px;
  display: flex;
  align-items: center;
  box-sizing: border-box;

  .el-date-editor.el-input,
  .el-date-editor.el-input__inner {
    max-width: 200px;
  }

  ::v-deep .el-input__inner {
    height: 38px;
    border: none;
  }

  ::v-deep .el-icon-arrow-down {
    color: #c2c2c2;
    padding: 0 10px 0 0;
  }

  ::v-deep .el-icon-arrow-up {
    color: #c2c2c2;
    padding: 0 10px 0 0;
  }
}

// 弹框样式
.dialog {
  ::v-deep .el-dialog {
    max-width: 750px;
    min-width: 500px;
    min-height: 426px;
    border-radius: 10px;
    background-color: #f9f9f9;
  }

  ::v-deep .el-dialog__header {
    height: 48px;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 18px;
    background-color: #4895db;
    padding: 0;
    border-radius: 10px 10px 0 0;

    span {
      font-size: 16px;
      color: #fff;
    }
  }

  ::v-deep .el-input__inner {
    height: 30px;
    line-height: 30px;
  }

  ::v-deep .el-dialog__headerbtn {
    top: 15px;
  }

  ::v-deep .el-dialog__headerbtn .el-dialog__close {
    color: #fff;
    font-size: 18px;
  }

  ::v-deep .el-dialog--center .el-dialog__body {
    text-align: initial;
    padding: 80px 0 100px 0;
    display: flex;
    justify-content: center;
    align-items: center;
  }

  ::v-deep .el-form-item__content {
    width: 280px;
  }

  ::v-deep .el-form-item {
    margin-bottom: 10px;
  }

  ::v-deep .el-dialog__footer {
    padding: 10px 20px 50px;
  }

  ::v-deep .el-button--default {
    width: 130px;
    border-radius: 20px;
    background-color: #f0f0f0;
    color: #aaaaaa;
    border: none;
  }

  ::v-deep .el-button + .el-button {
    width: 130px;
    border-radius: 20px;
    margin-left: 50px;
    background-color: #4895db;
    border-color: #4895db;
  }
}
</style>